home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2802 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.2 KB  |  71 lines

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Q:order of evaluation
  5. Date: 19 Jan 1996 19:39:04 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan19143904@g7240065.bridge.bst.bls.com>
  8. References: <4dfhlu$a33$1@mhafn.production.compuserve.com>
  9.     <hamilton-1801962045570001@dialup-147.austin.io.com>
  10. NNTP-Posting-Host: bstfirewall.bst.bls.com
  11. In-reply-to: hamilton@shokwave.com's message of Thu, 18 Jan 1996 20:45:57
  12.     -0600
  13.  
  14. In article <hamilton-1801962045570001@dialup-147.austin.io.com> hamilton@shokwave.com (Jim Hamilton) writes:
  15.  
  16. : In article <4dfhlu$a33$1@mhafn.production.compuserve.com>, Holger Maier
  17. : <100336.3326@CompuServe.COM> wrote:
  18.  
  19. :> Consider
  20. :> #include <iostream>
  21. :> int main() {
  22. :>   int i=1;int j=i+(i+=1);
  23. :>   cout<<i<<','<<j<<'\n';
  24. :>   return 0;
  25. :> }
  26.  
  27. : The highest precedence in any expression is the insides of parentheses
  28. : ().  Therefore (i+=1) is evaluated before i+().
  29.  
  30. No
  31.  
  32.   i + (i += 1)
  33.  
  34. Precedence alters the way the expression is parsed not the order in which
  35. it is evaluated
  36.  
  37.         With parenthisis                       Without parenthis 
  38.  
  39.                op(+)                               op(+) ignoring side effect
  40.                /  \                                /  \  which would make this
  41.               /    \                              /    \ an illegal construct
  42.              /      \                            /      \
  43.          expr(i)    op(+=)                    op(+)   expr(1)
  44.                     /  \                      /  \
  45.                    /    \                    /    \
  46.                   /      \                  /      \
  47.                expr(i)  expr(1)          expr(i)  expr(i)
  48.  
  49. if op is not a sequence point then it is implementation dependant on
  50. whether the left had side of the expression is evaluated before the
  51. right hand side of the expression. If op is a sequence point (like ',') then
  52. the left hand side is evaluated before the right.
  53.  
  54. so if i = 1 going into the expression, it can be evaluated
  55.  
  56.     1 + (i += 1)
  57.       1 + 2       /* i now equals 2 */
  58.         3
  59. or
  60.     i + 2         /* i now equals 2 */
  61.       2 + 2
  62.         4
  63.  
  64. Precedence does not dictate order.
  65.  
  66. Regards
  67.  
  68.    -A.
  69. -- 
  70. | A.Champion                |
  71.